local get_gray2 = function(device_id,percent)
    if nil == device_id then
        return false
    end
    local md5str = ngx.md5(ngx.md5(tostring(device_id)))
    local substr = "0x" .. string.lower(string.sub(md5str,1,3))                    --convert 3 bit char to Hex
    local cur_percent = math.ceil((string.format("%d",substr)/4096)*100)        --calculate sunstr's percentage in 3 bit all set
    if tonumber(cur_percent) <= tonumber(percent) then
        return true
    end
    return false
end


function ip2int(ip)
    local s,e = string.find(ip,"%.")
    if nil == s then
        return 0
    end
    local a = string.sub(ip,1,s-1)
    
    local ip1 = string.sub(ip,e+1,string.len(ip)+1)
    s,e = string.find(ip1,"%.")
    local b = string.sub(ip1,1,s-1)
    
    local ip2 = string.sub(ip1,e+1,string.len(ip1)+1)
    s,e = string.find(ip2,"%.")
    local c = string.sub(ip2,1,s-1)

    local d = string.sub(ip2,e+1,string.len(ip2)+1)

    --local ipnum = tonumber(a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d)

    --local nipnum = netutil.htonl(ipnum)
    --nlog.dinfo(a .. "-"..b.."-"..c.."-"..d.."-"..ipnum.."-"..nipnum.."-"..string.format("%X", ipnum).."-"..string.format("%X", nipnum))
    return  "0X" .. string.format("%02X", d) ..
                    string.format("%02X", c) ..
                    string.format("%02X", b) ..
                    string.format("%02X", a)
end

function get_client_app(client_app)
    local app_version = "5.21.1"
    if nil == client_app then
        client_app = ngx.var.http_user_agent
        if nil == client_app then
            return app_version
        end
    end
    local aa,bb = string.find(client_app,"@",1,true)
    if nil == aa then
        return app_version
    end
    client_app = string.sub(client_app,aa+1,aa+6)

    local _,_,ver = string.find(client_app,"^(%d%.[%d]+%.%d)" )
    -- 针对符合正则规范的， 通过截取获取版本号
    if ver and 5 == #ver then
        app_version = string.sub(ver,1,2) .. "0" .. string.sub(ver,3,5)
    else
        app_version = ver or app_version  -- 保证ver == nil，赋值为 "5.09.1"
    end
    return app_version
end
local args = ngx.req.get_uri_args()
local client_app = args.client_app or ""

local tmsg_ip = ""
-- local client_version =  get_client_app(client_app)
-- if client_version >= "5.21.3" then
    local remote_addr = ngx.var.remote_addr or ngx.time()
    if true == get_gray2(remote_addr, 50) then
        tmsg_ip = "cs-tmpmsg.intsig.net=" .. ip2int("54.193.170.51") .. "," .. ip2int("18.144.178.209") .. "\r\n"
    else
--        tmsg_ip = "cs-tmpmsg.intsig.net=" .. ip2int("54.177.56.112") .. "," .. ip2int("54.193.230.221") .. "\r\n"
        tmsg_ip = "cs-tmpmsg.intsig.net=" .. ip2int("18.144.178.209") .. "," .. ip2int("54.193.170.51") .. "\r\n"
    end
-- end

local str =  "[dns]\r\n" .. tmsg_ip ..
             "msgtcp2.intsig.net=".. ip2int("52.9.252.169")..","..ip2int("52.9.203.4").."\r\n" ..
             "d3133.intsig.net=" .. ip2int("54.176.154.188") .. "\r\n"

ngx.header.Content_Type = "text/plain"
ngx.header.Content_Length = string.len(str)
ngx.print(str)
